******************************************************************************
*										
*                           Boese & Eberhardt							
*            
*                          Facets of Democracy
*													
******************************************************************************
*                   PCDID - heterogeneous model (1959-2018)
*			    (Specification with exports/trade & pop growth)
* 				   High and mid-level indices: (1) Lib_dem,
*			       (2) Polyarchy, (3) Liberal component
******************************************************************************
*                                   Graphs 
*                       Using standardized thresholds 
******************************************************************************
*                           Created: 5th June 2024					
*                      Last Changed: 17th July 2024					
******************************************************************************
*                              Markus Eberhardt						
******************************************************************************
*                 School of Economics, University of Nottingham,			
*                  University Park, Nottingham NG7 2RD, England			
*                     email: markus.eberhardt@nottingham.ac.uk				
*                   web: https://sites.google.com/site/medevecon/			
******************************************************************************

*** This code produces
*** Figure 2, Panels (a) and (b)
*** Figure 3, Panels (a), (b) and (c)
*** Draft June 2024

clear all
set matsize 11000

***
*** Paths
***

global path "D:/Dropbox/Facets of Democracy"
global rawpath "D:/Dropbox"

global path "/Users/lezme/Dropbox/Facets of Democracy"
global rawpath "/Users/lezme/Dropbox"

global data "$path/Data"
global dofolder "$path/Do_files"
global otherdata "$path/Data"
global outputfolder "$path/Output"
global texfolder "$rawpath/Apps/Overleaf/Facets of Democracy"
global texfolder2 "$path/Tex"

******	
***********
*********** Multiple (high-level) indicators 
***********
******

use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_p4_6_diagnostics_4.dta", clear 
rename beta1 beta
replace beta=. if beta==0
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_p4_6
}
drop nwbcode 
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_p4_1_diagnostics_4.dta", sort 
rename beta1 beta
replace beta=. if beta==0
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_p4_1
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_row2_diagnostics_4.dta", sort 
rename beta1 beta
replace beta=. if beta==0
drop regime_def _merge
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_row2
}
drop nwbcode
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_0_diagnostics_4.dta", sort 
rename beta1 beta
replace beta=. if beta==0
drop regime_def _merge
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld
}
drop nwbcode
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_boix_diagnostics_4.dta", sort 
rename beta1 beta
replace beta=. if beta==0
drop regime_def _merge
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_boix
}

* PolityIV positive; N=82
tab startyear_p4_1, gen(by_)
mrunning beta_p4_1 exposure_p4_1 flips_p4_1, gen(change_) ci gense(changese_) adjust(by_*) nograph
drop change_3-change_17 changese_3-changese_17 
drop change_2 changese_2
rename change_1 change_p4_1
rename changese_1 changese_p4_1
gen tstat_p4_1 = change_p4_1/changese_p4_1

* PolityIV 6; N=73
drop by_*
tab startyear_p4_6, gen(by_)
mrunning beta_p4_6 exposure_p4_6 flips_p4_6, gen(change_) ci gense(changese_) adjust(by_*) nograph
rename change_1 change_p4_6
rename changese_1 changese_p4_6
drop change_2 changese_2 
drop change_3-change_16 changese_3-changese_16
gen tstat_p4_6 = change_p4_6/changese_p4_6

* ROW 2; N=74
drop by_*
tab startyear_row2, gen(by_)
mrunning beta_row2 exposure_row2 flips_row2, gen(change_) ci gense(changese_) adjust(by_*) nograph
rename change_1 change_row2
rename changese_1 changese_row2
drop change_2 changese_2 
drop change_3-change_17 changese_3-changese_17 
gen tstat_row2 = change_row2/changese_row2

* LibDem mean; N=63
drop by_*
tab startyear_ld, gen(by_)
mrunning beta_ld exposure_ld flips_ld, gen(change_) ci gense(changese_) adjust(by_*) nograph
rename change_1 change_ld
rename changese_1 changese_ld
drop change_2 changese_2 
drop change_3-change_16 changese_3-changese_16
gen tstat_ld = change_ld/changese_ld

* Boix - N=65
drop by_*
tab startyear_boix, gen(by_)
mrunning beta_boix exposure_boix flips_boix, gen(change_) ci gense(changese_) adjust(by_*) nograph
rename change_1 change_boix
rename changese_1 changese_boix
drop change_2 changese_2 
drop change_3-change_19 changese_3-changese_19
gen tstat_boix = change_boix/changese_boix


twoway ///
	(line change_row2 exposure_row2, sort ///		1
		lw(.6) lcolor(midblue*1.5) lpattern(solid)) ///	
	(scatter change_row2 exposure_row2 if abs(tstat_row2)<1.645, sort jitterseed(280115) jitter(2) ///	2
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter change_row2 exposure_row2 if abs(tstat_row2)>1.645, sort jitterseed(280115) jitter(2) ///	3
		 msymbol(O)  msize(medlarge) mfcolor(midblue%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(line change_boix exposure_boix, sort ///	4
		lw(.6) lcolor(gold*1) lpattern(solid)) ///
	(scatter change_boix exposure_boix if abs(tstat_boix)<1.645, sort jitterseed(280115) jitter(2) ///	5
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(gold*1.25%60) mlwidth(thin)) ///
	(scatter change_boix exposure_boix if abs(tstat_boix)>1.645, sort jitterseed(280115) jitter(2) ///	6
		 msymbol(O)  msize(medlarge) mfcolor(gold%60) mlcolor(gold*1.25%60) mlwidth(thin)) ///
	(line change_p4_1 exposure_p4_1, sort yaxis(2) ///	14
		lw(.6) lcolor(navy*1.5) lpattern(solid)) ///
	(scatter change_p4_1 exposure_p4_1 if abs(tstat_p4_1)<1.645, sort jitterseed(280115) jitter(2) ///	7
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(navy*1.5%60) mlwidth(thin)) ///
	(scatter change_p4_1 exposure_p4_1 if abs(tstat_p4_1)>1.645, sort jitterseed(280115) jitter(2) ///	8
		 msymbol(O)  msize(medlarge) mfcolor(navy%60) mlcolor(navy*1.5%60) mlwidth(thin)) ///
	(line change_p4_6 exposure_p4_6, sort /// 	9
		lw(.6) lcolor(teal*1) lpattern(solid)) ///
	(scatter change_p4_6 exposure_p4_6 if abs(tstat_p4_6)<1.645, sort jitterseed(280115) jitter(2) ///	10
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(teal%60) mlwidth(thin)) ///
	(scatter change_p4_6 exposure_p4_6 if abs(tstat_p4_6)>1.645, sort jitterseed(280115) jitter(2) ///	11
		 msymbol(O)  msize(medlarge) mfcolor(teal%60) mlcolor(teal%60) mlwidth(thin)) ///
	(line change_ld exposure_ld, sort yaxis(2) ///	15
		lw(.6) lcolor(orange*1.25) lpattern(solid)) ///
	(scatter change_ld exposure_ld if abs(tstat_ld)<1.645, sort jitterseed(280115) jitter(2) ///	12
		 msymbol(Oh)  msize(medlarge) mfcolor(none) mlcolor(orange*1.5) mlwidth(thin)) ///
	(scatter change_ld exposure_ld if abs(tstat_ld)>1.645, sort jitterseed(280115) jitter(2) ///	13
		 msymbol(O)  msize(medlarge) mfcolor(orange*1) mlcolor(orange*1.5) mlwidth(thin)) ///
, xsize(9) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(medsmall)) ///
	ytitle("Democracy Effect (in %)", size(medsmall))  ///
	ytitle("Democracy Effect (in %)", size(medsmall) axis(2))  ///
	yscale(range(-20.1 32.1))  yscale(range(-20.1 32.1) axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white)) ///
	xmtick(##5) 	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash)) ///
	legend(rows(2) order(- "Statistical Sign. (10% level):" 13 8 11 3 6   -  "[Sample treated/control]:" 15 14 9 1 4 ) ///
	label(3 "ROW: >1") label(6 "Boix") label(8 "PolityIV: >0") label(11 "PolityIV: >5") label(13 "LibDem: >mean")  ///
	label(1 "[74/52]")	label(4 "[65/50]")	label(14 "[82/36]") label(9 "[73/50]") label(15 "[63/59]") 	size(small) symxsize(*.5))	
graph export "$texfolder/PCDID_Maddison2024_five_top_levels_st.png", as(png) replace width(1200)
graph export "$texfolder/PCDID_Maddison2024_five_top_levels_st.eps", as(eps) replace


* Some stats on median sample size and time in demo 
tabstat total_obs_p4_1 total_obs_p4_6 total_obs_row2 total_obs_ld total_obs_boix, statistic(median)
tabstat exposure_p4_1 exposure_p4_6 exposure_row2 exposure_boix exposure_ld, statistic(median)

******	
***********
*********** Liberal Democracy (various cutoffs) 
***********
******

use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_neg25_diagnostics_4.dta", clear 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld_25
}
drop nwbcode 
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_neg125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def _merge
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld_125
}
drop nwbcode
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_0_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld0
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld125
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_ld_25_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_ld25
}

local z "ld"
foreach kk of local z{
	local zz "_125 _25 0 125 25"
	foreach k of local zz{
	tab startyear_`kk'`k', gen(by_)
	mrunning beta_`kk'`k' exposure_`kk'`k' flips_`kk'`k', gen(changeX_) adjust(by_*) ci gense(changeseX_)  nograph
	rename changeX_1 change_`kk'`k'
	rename changeseX_1 changese_`kk'`k'
	drop changeX_* changeseX_*
	gen tstat_`kk'`k' = change_`kk'`k'/changese_`kk'`k'
	drop by_*
	}
}

twoway ///
	(line change_ld_25 exposure_ld_25, sort ///
		lcolor(gs2%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_ld_25 exposure_ld_25 if abs(tstat_ld_25)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs2%60) mlwidth(thin)) ///
	(scatter change_ld_25 exposure_ld_25 if abs(tstat_ld_25)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs2%60) mlcolor(gs2%60) mlwidth(thin)) ///
	(line change_ld_125 exposure_ld_125, sort ///
		lcolor(gs5%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_ld_125 exposure_ld_125 if abs(tstat_ld_125)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs5%60) mlwidth(thin)) ///
	(scatter change_ld_125 exposure_ld_125 if abs(tstat_ld_125)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs5%60) mlcolor(gs5%60) mlwidth(thin)) ///
	(line change_ld125 exposure_ld125, sort ///
		lcolor(gs8%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_ld125 exposure_ld125 if abs(tstat_ld125)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs8%60) mlwidth(thin)) ///
	(scatter change_ld125 exposure_ld125 if abs(tstat_ld125)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs8%60) mlcolor(gs8%60) mlwidth(thin)) ///
	(line change_ld25 exposure_ld25, sort ///
		lcolor(gs11%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_ld25 exposure_ld25 if abs(tstat_ld25)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs11%60) mlwidth(thin)) ///
	(scatter change_ld25 exposure_ld25 if abs(tstat_ld25)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs11%60) mlcolor(gs11%60) mlwidth(thin)) ///
	(line change_ld0 exposure_ld0, sort yaxis(2) ///
		lcolor(orange*1.25) lwidth(.8) lpattern(solid)) ///
	(scatter change_ld0 exposure_ld0 if abs(tstat_ld0)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(white%40) mlcolor(orange*1.5) mlwidth(thin)  yaxis(2)) ///
	(scatter change_ld0 exposure_ld0 if abs(tstat_ld0)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(orange*1) mlcolor(orange*1.5) mlwidth(thin)  yaxis(2)) ///
, xsize(9) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Regime (Length of Treatment)", size(medsmall)) ///
	ytitle("Democracy Effect (in %)", size(medsmall))  ///
	yscale(range(-20.1 32.1))  yscale(range(-20.1 32.1) axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white)) ///
	xmtick(##5) 	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash)) ///
	ytitle("Democracy Effect (in %)", size(medsmall) axis(2))  ///
	legend(rows(2) order(- "Democracy Indicator Cut-off (Sign. at 10% level):" 3 6 15 9 12 - "[Sample treated/control]:" ///
	1 4 13 7 10) ///
	label(3 "-1/4 sd")  label(6 "-1/8 sd") label(15 "mean") label(9 "+1/8 sd") label(12 "+1/4 sd") ///
	label(1 "[77/43]") label(4 "[69/51]") label(13 "[63/59]") label(7 "[62/64]") label(10 "[60/66]") ///
	size(small) symxsize(*.5)) xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_libdem_multiple_st.png", as(png) replace width(1200)
graph export "$texfolder/PCDID_Maddison2024_libdem_multiple_st.eps", as(eps) replace 


******	
***********
*********** Polyarchy (various cutoffs) 
***********
******

use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_neg25_diagnostics_4.dta", clear 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly_25
}
drop nwbcode
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_neg125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly_125
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_0_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly0
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly125
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_25_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly25
}


local z "poly"
foreach kk of local z{
	local zz "_25 _125 0 125 25"
	foreach k of local zz{
	tab startyear_`kk'`k', gen(by_)
	mrunning beta_`kk'`k' exposure_`kk'`k' flips_`kk'`k', gen(changeX_) adjust(by_*) ci gense(changeseX_)  nograph
	rename changeX_1 change_`kk'`k'
	rename changeseX_1 changese_`kk'`k'
	drop changeX_* changeseX_*
	gen tstat_`kk'`k' = change_`kk'`k'/changese_`kk'`k'
	drop by_*
	}
}

twoway ///
	(line change_poly_25 exposure_poly_25, sort ///
		lcolor(gs2%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_poly_25 exposure_poly_25 if abs(tstat_poly_25)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs2%60) mlwidth(thin)) ///
	(scatter change_poly_25 exposure_poly_25 if abs(tstat_poly_25)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs2%60) mlcolor(gs2%60) mlwidth(thin)) ///
	(line change_poly_125 exposure_poly_125, sort ///
		lcolor(gs5%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_poly_125 exposure_poly_125 if abs(tstat_poly_125)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs5%60) mlwidth(thin)) ///
	(scatter change_poly_125 exposure_poly_125 if abs(tstat_poly_125)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs5%60) mlcolor(gs5%60) mlwidth(thin)) ///
	(line change_poly125 exposure_poly125, sort ///
		lcolor(gs8%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_poly125 exposure_poly125 if abs(tstat_poly125)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs8%60) mlwidth(thin)) ///
	(scatter change_poly125 exposure_poly125 if abs(tstat_poly125)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs8%60) mlcolor(gs8%60) mlwidth(thin)) ///
	(line change_poly25 exposure_poly25, sort ///
		lcolor(gs11%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_poly25 exposure_poly25 if abs(tstat_poly25)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs11%60) mlwidth(thin)) ///
	(scatter change_poly25 exposure_poly25 if abs(tstat_poly25)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs11%60) mlcolor(gs11%60) mlwidth(thin)) ///
	(line change_poly0 exposure_poly0, sort yaxis(2) ///
		lcolor(pink*1.5) lwidth(.8) lpattern(solid)) ///
	(scatter change_poly0 exposure_poly0 if abs(tstat_poly0)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(white%40) mlcolor(pink*1.5) mlwidth(thin)  yaxis(2)) ///
	(scatter change_poly0 exposure_poly0 if abs(tstat_poly0)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(pink*1) mlcolor(pink*1.5) mlwidth(thin)  yaxis(2)) ///
, xsize(9) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Regime (Length of Treatment)", size(medsmall)) ///
	ytitle("Democracy Effect (in %)", size(medsmall))  ///
	yscale(range(-10.1 30.1))  ///
	ylabel(-10(10)30, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	ylabel(-10(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white)) ///
	ytitle("Democracy Effect (in %)", size(medsmall) axis(2))  ///
	yscale(range(-10.1 30.1) axis(2)) 	xmtick(##5) ///
	legend(rows(2) order(- "Polyarchy Indicators for Cut-offs at" 1 4 13 7 10 - "Significant at 10% level (Treated Countries)" ///
	3 6 15 9 12) ///
	label(1 "-1/4 sd")  label(4 "-1/8 sd") label(13 "mean") label(7 "+1/8 sd") label(10 "+1/4 sd") ///
	label(3 "(85)") label(6 "(80)") label(15 "(77)") label(9 "(76)") label(12 "(69)") ///
	size(small) symxsize(*.5)) xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_poly_multiple_st.png", as(png) replace width(1200)
graph export "$texfolder/PCDID_Maddison2024_poly_multiple_st.eps", as(eps) replace 
	
* Some stats on median sample size and time in demo 
tabstat total_obs_poly0 exposure_poly0, statistic(median)
	
	
******	
***********
*********** Liberal Component (various cutoffs) 
***********
******

use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_neg25_diagnostics_4.dta", clear 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib_25
}
drop nwbcode
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_neg125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib_125
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_0_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib0
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_25_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib25
}
drop nwbcode _merge
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_125_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib125
}

local z "lib"
foreach kk of local z{
	local zz "_25 _125 0 125 25"
	foreach k of local zz{
	tab startyear_`kk'`k', gen(by_)
	mrunning beta_`kk'`k' exposure_`kk'`k' flips_`kk'`k', gen(changeX_) adjust(by_*) ci gense(changeseX_)  nograph
	rename changeX_1 change_`kk'`k'
	rename changeseX_1 changese_`kk'`k'
	drop changeX_* changeseX_*
	gen tstat_`kk'`k' = change_`kk'`k'/changese_`kk'`k'
	drop by_*
	}
}


twoway ///
	(line change_lib_25 exposure_lib_25 if exposure_lib_25<69, sort ///
		lcolor(gs2%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_lib_25 exposure_lib_25 if abs(tstat_lib_25)<1.645 & exposure_lib_25<69, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs2%60) mlwidth(thin)) ///
	(scatter change_lib_25 exposure_lib_25 if abs(tstat_lib_25)>1.645 & exposure_lib_25<69, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs2%60) mlcolor(gs2%60) mlwidth(thin)) ///
	(line change_lib_125 exposure_lib_125 if exposure_lib_25<69, sort ///
		lcolor(gs5%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_lib_125 exposure_lib_125 if abs(tstat_lib_125)<1.645  & exposure_lib_25<69, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs5%60) mlwidth(thin)) ///
	(scatter change_lib_125 exposure_lib_125 if abs(tstat_lib_125)>1.645  & exposure_lib_25<69, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs5%60) mlcolor(gs5%60) mlwidth(thin)) ///
	(line change_lib125 exposure_lib125, sort ///
		lcolor(gs8%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_lib125 exposure_lib125 if abs(tstat_lib125)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs8%60) mlwidth(thin)) ///
	(scatter change_lib125 exposure_lib125 if abs(tstat_lib125)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs8%60) mlcolor(gs8%60) mlwidth(thin)) ///
	(line change_lib25 exposure_lib25, sort ///
		lcolor(gs11%60) lwidth(.8) lpattern(solid)) ///
	(scatter change_lib25 exposure_lib25 if abs(tstat_lib25)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%40) mlcolor(gs11%60) mlwidth(thin)) ///
	(scatter change_lib25 exposure_lib25 if abs(tstat_lib25)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(gs11%60) mlcolor(gs11%60) mlwidth(thin)) ///
	(line change_lib0 exposure_lib0, sort yaxis(2) ///
		lcolor(midblue*1.5) lwidth(.8) lpattern(solid)) ///
	(scatter change_lib0 exposure_lib0 if abs(tstat_lib0)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(white%40) mlcolor(midblue*1.5) mlwidth(thin)  yaxis(2)) ///
	(scatter change_lib0 exposure_lib0 if abs(tstat_lib0)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(large) mfcolor(midblue*1) mlcolor(midblue*1.5) mlwidth(thin)  yaxis(2)) ///
, xsize(9) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Regime (Length of Treatment)", size(medsmall)) ///
	ytitle("Democracy Effect (in %)", size(medsmall))  ///
	yscale(range(-12.1 30.1))  ///
	ylabel(-10(10)30, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	ylabel(-10(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white)) ///
	ytitle("Democracy Effect (in %)", size(medsmall) axis(2))  ///
	yscale(range(-12.1 30.1) axis(2)) 	xmtick(##5) ///
	legend(rows(2) order(- "Liberal Component Indicators for Cut-offs at" 1 4 13 7 10 - "Significant at 10% level (Treated Countries)" ///
	3 6 15 9 12) ///
	label(1 "-1/4 sd")  label(4 "-1/8 sd") label(13 "mean") label(7 "+1/8 sd") label(10 "+1/4 sd") ///
	label(3 "(81)") label(6 "(74)") label(15 "(72)") label(9 "(65)") label(12 "(65)") ///
	size(medsmall) symxsize(*.5)) xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash)) 
graph export "$texfolder/PCDID_Maddison2024_lib_multiple_st.png", as(png) replace width(1200)
graph export "$texfolder/PCDID_Maddison2024_lib_multiple_st.eps", as(eps) replace 	
	
* Some stats on median sample size and time in demo 
tabstat total_obs_lib0 exposure_lib0, statistic(median)
	
	
******	
***********
*********** Horserace: poly versus liberal 
***********
******
	

****
**** Helper for poly vs liberal
****	

use "$data/madd_ert_vdem_2021_helper2.dta", clear 
tsset nwbcode year
keep if year>=1959 & year<=2018
sort nwbcode year
gen demevent_poly = 1 if ((dem_poly==1 & l.dem_poly==0)) & sample==1
gen demevent_lib = 1 if ((dem_lib==1 & l.dem_lib==0)) & sample==1
replace demevent_poly = demevent_poly*v2x_liberal if sample==1
replace demevent_lib = demevent_lib*v2x_polyarchy if sample==1
gen demsd_poly = dem_poly*st_liberal if sample==1
gen demsd_lib = dem_lib*st_polyarchy if sample==1

sort wbcode year
collapse (mean) demevent_poly demevent_lib (sd) demsd_poly demsd_lib, by(wbcode)
replace demsd_poly = . if demsd_poly==0
replace demsd_lib = . if demsd_lib==0
sort wbcode
save "$data/poly_lib_moments2new.dta", replace 
* Captures the polyarchy and liberal component index values in the year of democratisation.
* If there are more than one episode, it capures the mean.
* Also provides the std of the polyarchy and liberal component indeces during `democracy' (0.5 cutoff)
* demevent_poly has the liberal component values (mean) of the polyarchy threshold years
* demevent_lib has the polyarchy values (mean) of the liberal component threshold years
	

use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_0_diagnostics_4.dta", clear 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_poly
}
drop nwbcode 
merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_0_diagnostics_4.dta", sort 
rename beta1 beta
drop regime_def
local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
foreach k of local z{
	rename `k' `k'_lib
}

tab startyear_poly, gen(by_)
tab startyear_lib, gen(b_y_)
	
	
*** Horseraces
drop _merge
sort wbcode
merge wbcode using "$data/poly_lib_moments2new.dta", sort 
drop if missing(startyear_poly) & missing(startyear_lib)

* Running line regressions condition on the regime change year value and in treatment sd of the respective democracy index
mrunning beta_poly exposure_poly flips_poly demevent_poly demsd_poly, ///
	gen(changeX_) ci gense(changeseX_) adjust(by_*)   nograph
mrunning beta_lib exposure_lib  flips_lib demevent_lib demsd_lib, ///
	gen(change_) ci gense(changese_) adjust(b_y_*)   nograph
rename changeX_1 changeX_poly
rename changeseX_1 changeseX_poly
rename change_1 changeX_lib
rename changese_1 changeseX_lib
gen tstatX_poly = changeX_poly/changeseX_poly
gen tstatX_lib = changeX_lib/changeseX_lib

twoway ///
	(hist exposure_lib if !missing(beta_lib), yaxis(2) w(5) start(0) freq fcolor(gs12%80) lcolor(gs12%80) barwidth(4.5)) ///
	(hist exposure_poly if !missing(beta_poly), yaxis(2) w(5) start(0) freq fcolor(gs7%80) lcolor(gs7%80) barwidth(2)) ///
	(line changeX_poly exposure_poly, sort ///
		lw(.8) lcolor(pink*1.5) lpattern(solid)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(pink) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(line changeX_lib exposure_lib, sort ///
		lw(.8) lcolor(midblue*1.5) lpattern(dash)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(midblue) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
, xsize(9) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(medsmall)) ///
	ytitle("Conditional Democracy Effect (in %)", size(medsmall))  ///
	ytitle("Country Count", size(medsmall) axis(2))  ///
	yscale(range(-0.1 32.1))  xmtick(##5) ymtick(##2, axis(2)) ///
	ylabel(0(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(0(2)12, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white)) ///
	legend(rows(2) order(- "Democracy Effect:" 3 6 - "Country Count:" 2 - "Significant (10% level)" 5 8 1) ///
	label(10 "") label(3 "Polyarchy > mean") label(6 "Liberal > mean") ///
	label(5 "")	label(8 "")	label(1 "Liberal (68)") label(2 "Polyarchy (75)") size(small)  symxsize(*.5) holes(6 10)) ///
	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_st.png", as(png) replace width(1200)
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_st.eps", as(eps) replace


******	
***********
*********** Horserace: poly versus liberal  - different cutoffs 
***********
******
	
	
local sdvari "neg25 neg125 0 125 25"

foreach kk of local sdvari{
	
	use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_poly_`kk'_diagnostics_4.dta", clear 
	rename beta1 beta
	drop regime_def
	local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
	foreach k of local z{
		rename `k' `k'_poly
	}
	drop nwbcode 
	merge wbcode using "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_st_multiple_lib_`kk'_diagnostics_4.dta", sort 
	rename beta1 beta
	drop regime_def
	local z "startyear endyear beta flips democratisations exposure total_obs nonexposure"
	foreach k of local z{
		rename `k' `k'_lib
	}

	tab startyear_poly, gen(by_)
	tab startyear_lib, gen(b_y_)

	*** Horseraces
	drop _merge
	sort wbcode
	merge wbcode using "$data/poly_lib_moments2new.dta", sort 
	drop if missing(startyear_poly) & missing(startyear_lib)

	* Running line regressions condition on the regime change year value and in treatment sd of the respective democracy index
	mrunning beta_poly exposure_poly flips_poly demevent_poly demsd_poly, ///
		gen(changeX_) ci gense(changeseX_) adjust(by_*)   nograph
	mrunning beta_lib exposure_lib  flips_lib demevent_lib demsd_lib, ///
		gen(change_) ci gense(changese_) adjust(b_y_*)   nograph
	rename changeX_1 changeX_poly
	rename changeseX_1 changeseX_poly
	rename change_1 changeX_lib
	rename changese_1 changeseX_lib
	gen tstatX_poly = changeX_poly/changeseX_poly
	gen tstatX_lib = changeX_lib/changeseX_lib

	save "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_mid_level_st_`kk'.dta", replace
	
}



*** Mean - 1/4 SD
use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_mid_level_st_neg25.dta", replace

twoway ///
	(hist exposure_lib if !missing(beta_lib), yaxis(2) w(5) start(0) freq fcolor(gs12%80) lcolor(gs12%80) barwidth(4.5)) ///
	(hist exposure_poly if !missing(beta_poly), yaxis(2) w(5) start(0) freq fcolor(gs7%80) lcolor(gs7%80) barwidth(2)) ///
	(line changeX_poly exposure_poly, sort ///
		lw(.8) lcolor(pink*1.5) lpattern(solid)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(pink) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(line changeX_lib exposure_lib, sort ///
		lw(.8) lcolor(midblue*1.5) lpattern(dash)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(midblue) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
, xsize(5) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(small)) ///
	ytitle("Conditional Democracy Effect (in %)", size(small))  ///
	ytitle("Country Count", size(small) axis(2))  ///
	yscale(range(-20.1 32.1))  xmtick(##5) ymtick(##2, axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(0(2)18, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white) margin(zero)) plotregion(margin(zero)) ///
	legend(rows(2) order(- "Democracy Effect:" 3 6 - "Countries:" 2 - "Sign. 10% level (N)" 5 8 1) ///
	label(10 "") label(3 "Poly") label(6 "Liberal") ///
	label(5 "(71)")	label(8 "(67)")	label(1 "Liberal") label(2 "Poly") size(small)  symxsize(*.5) holes(6 10)) ///
	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_neg25.eps", as(eps) replace

sum changeX_lib changeX_poly

*** Mean - 1/8 SD
use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_mid_level_st_neg125.dta", replace

twoway ///
	(hist exposure_lib if !missing(beta_lib), yaxis(2) w(5) start(0) freq fcolor(gs12%80) lcolor(gs12%80) barwidth(4.5)) ///
	(hist exposure_poly if !missing(beta_poly), yaxis(2) w(5) start(0) freq fcolor(gs7%80) lcolor(gs7%80) barwidth(2)) ///
	(line changeX_poly exposure_poly, sort ///
		lw(.8) lcolor(pink*1.5) lpattern(solid)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(pink) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(line changeX_lib exposure_lib, sort ///
		lw(.8) lcolor(midblue*1.5) lpattern(dash)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(midblue) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
, xsize(5) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(small)) ///
	ytitle("Conditional Democracy Effect (in %)", size(small))  ///
	ytitle("Country Count", size(small) axis(2))  ///
	yscale(range(-20.1 32.1))  xmtick(##5) ymtick(##2, axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(0(2)18, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white) margin(zero)) plotregion(margin(zero)) ///
	legend(rows(2) order(- "Democracy Effect:" 3 6 - "Countries:" 2 - "Sign. 10% level (N)" 5 8 1) ///
	label(10 "") label(3 "Poly") label(6 "Liberal") ///
	label(5 "(74)")	label(8 "(67)")	label(1 "Liberal") label(2 "Poly") size(small)  symxsize(*.5) holes(6 10)) ///
	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_neg125.eps", as(eps) replace

sum changeX_lib changeX_poly


*** Mean + 1/8 SD
use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_mid_level_st_125.dta", replace


twoway ///
	(hist exposure_lib if !missing(beta_lib), yaxis(2) w(5) start(0) freq fcolor(gs12%80) lcolor(gs12%80) barwidth(4.5)) ///
	(hist exposure_poly if !missing(beta_poly), yaxis(2) w(5) start(0) freq fcolor(gs7%80) lcolor(gs7%80) barwidth(2)) ///
	(line changeX_poly exposure_poly, sort ///
		lw(.8) lcolor(pink*1.5) lpattern(solid)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(pink) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(line changeX_lib exposure_lib, sort ///
		lw(.8) lcolor(midblue*1.5) lpattern(dash)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(midblue) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
, xsize(5) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(small)) ///
	ytitle("Conditional Democracy Effect (in %)", size(small))  ///
	ytitle("Country Count", size(small) axis(2))  ///
	yscale(range(-20.1 32.1))  xmtick(##5) ymtick(##2, axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(0(2)12, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white) margin(zero)) plotregion(margin(zero)) ///
	legend(rows(2) order(- "Democracy Effect:" 3 6 - "Countries:" 2 - "Sign. 10% level (N)" 5 8 1) ///
	label(10 "") label(3 "Poly") label(6 "Liberal") ///
	label(5 "(70)")	label(8 "(61)")	label(1 "Liberal") label(2 "Poly") size(small)  symxsize(*.5) holes(6 10)) ///
	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_125.eps", as(eps) replace

sum changeX_lib changeX_poly


*** Mean + 1/4 SD
use "$outputfolder/PCDID_Maddison2024_Drilling_1959_2018_mid_level_st_25.dta", replace

twoway ///
	(hist exposure_lib if !missing(beta_lib), yaxis(2) w(5) start(0) freq fcolor(gs12%80) lcolor(gs12%80) barwidth(4.5)) ///
	(hist exposure_poly if !missing(beta_poly), yaxis(2) w(5) start(0) freq fcolor(gs7%80) lcolor(gs7%80) barwidth(2)) ///
	(line changeX_poly exposure_poly, sort ///
		lw(.8) lcolor(pink*1.5) lpattern(solid)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(scatter changeX_poly exposure_poly if abs(tstatX_poly)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(pink) mlcolor(pink*1.5%60) mlwidth(thin)) ///
	(line changeX_lib exposure_lib, sort ///
		lw(.8) lcolor(midblue*1.5) lpattern(dash)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)<1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(white%60) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
	(scatter changeX_lib exposure_lib if abs(tstatX_lib)>1.645, sort jitterseed(280115) jitter(2) ///
		 msymbol(O)  msize(medlarge) mfcolor(midblue) mlcolor(midblue*1.5%60) mlwidth(thin)) ///
, xsize(5) ysize(4) scheme(s2mono) yline(0, lpattern(shortdash) lw(.4) lcolor(black)) ///
	xtitle("Years Spent in Democracy (Length of Treatment)", size(small)) ///
	ytitle("Conditional Democracy Effect (in %)", size(small))  ///
	ytitle("Country Count", size(small) axis(2))  ///
	yscale(range(-20.1 32.1))  xmtick(##5) ymtick(##2, axis(2)) ///
	ylabel(-20(10)30, labsize(small) glcolor(gs8) grid glwidth(.2) glp(shortdash)  angle(0)) ///
	ylabel(0(2)12, labsize(small) glcolor(gs8) angle(0) axis(2)) ///
	xlabel(0(5)60, labsize(small)) graphregion(color(white) margin(zero)) plotregion(margin(zero)) ///
	legend(rows(2) order(- "Democracy Effect:" 3 6 - "Countries:" 2 - "Sign. 10% level (N)" 5 8 1) ///
	label(10 "") label(3 "Poly") label(6 "Liberal") ///
	label(5 "(63)")	label(8 "(58)")	label(1 "Liberal") label(2 "Poly") size(small)  symxsize(*.5) holes(6 10)) ///
	xline(5 55, lcolor(gs8) lwidth(.2) lp(shortdash))
graph export "$texfolder/PCDID_Maddison2024_mid_levels_horseraces_25.eps", as(eps) replace

sum changeX_lib changeX_poly

